package com.foreveross.plugin.scanner;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONException;

import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;

import com.darin.camera.Scanner;

/**
 * @author Jianfeng.Lao
 * 
 */
public class ScannerPlugin extends CordovaPlugin {
	private static final String TAG = "ScanerPlugin";
	private CallbackContext callbackContext;

	@Override
	public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext)
			throws JSONException {
		this.callbackContext = callbackContext;
		Intent intent = new Intent(cordova.getActivity(), ScannerActivity.class);
		cordova.startActivityForResult(this, intent, 0);
		PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
		mPlugin.setKeepCallback(true);
		ScannerPlugin.this.callbackContext.sendPluginResult(mPlugin);

		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		Log.v(TAG, "onActivityResult" + intent);
		String result;
		if (intent != null) {
			result = intent.getStringExtra("result");
			callbackContext.success(result);
		}
	}

}
